/*
 * Copyright (C) 2010 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.zxing.client.ohos;

import ohos.aafwk.ability.Ability;
import ohos.app.Context;
import ohos.global.resource.RawFileEntry;
import ohos.media.audio.AudioStreamInfo;
import ohos.media.player.Player;
import ohos.vibrator.agent.VibratorAgent;

import java.io.IOException;

/**
 * Manages beeps and vibrations.
 */
public final class BeepManager {

    private static final String TAG = BeepManager.class.getSimpleName();

    private static final float BEEP_VOLUME = 0.10f;
    private static final long VIBRATE_DURATION = 200L;

    private final Context context;

    private boolean beepEnabled = true;
    private boolean vibrateEnabled = false;

    public BeepManager(Ability activity) {
        // We do not keep a reference to the Ability itself, to prevent leaks
        this.context = activity.getApplicationContext();
    }

    public boolean isBeepEnabled() {
        return beepEnabled;
    }

    /**
     * Call updatePrefs() after setting this.
     *
     * If the device is in silent mode, it will not beep.
     *
     * @param beepEnabled true to enable beep
     */
    public void setBeepEnabled(boolean beepEnabled) {
        this.beepEnabled = beepEnabled;
    }

    public boolean isVibrateEnabled() {
        return vibrateEnabled;
    }

    /**
     * Call updatePrefs() after setting this.
     *
     * @param vibrateEnabled true to enable vibrate
     */
    public void setVibrateEnabled(boolean vibrateEnabled) {
        this.vibrateEnabled = vibrateEnabled;
    }

//    @SuppressLint("MissingPermission")
    public synchronized void playBeepSoundAndVibrate() {
        if (beepEnabled) {
            playBeepSound();
        }
        if (vibrateEnabled) {
            VibratorAgent vibratorAgent = new VibratorAgent();
            vibratorAgent.startOnce((int) VIBRATE_DURATION);
        }
    }


//    public MediaPlayer playBeepSound() {
    public Player playBeepSound() {
        Player mediaPlayer = new Player(context);
        mediaPlayer.setAudioStreamType(AudioStreamInfo.StreamType.STREAM_TYPE_MUSIC.getValue());
        RawFileEntry rawFileEntry = context.getResourceManager().getRawFileEntry("zxing_beep.ogg");
        try {
            mediaPlayer.setSource(rawFileEntry.openRawFileDescriptor());
            mediaPlayer.setPlayerCallback(new Player.IPlayerCallback() {
                @Override
                public void onPrepared() {
                    mediaPlayer.play();
                }

                @Override
                public void onMessage(int i, int i1) {

                }

                @Override
                public void onError(int i, int i1) {
                    mediaPlayer.stop();
                    mediaPlayer.release();
                }

                @Override
                public void onResolutionChanged(int i, int i1) {

                }

                @Override
                public void onPlayBackComplete() {
                    mediaPlayer.stop();
                    mediaPlayer.release();
                }

                @Override
                public void onRewindToComplete() {

                }

                @Override
                public void onBufferingChange(int i) {

                }

                @Override
                public void onNewTimedMetaData(Player.MediaTimedMetaData mediaTimedMetaData) {

                }

                @Override
                public void onMediaTimeIncontinuity(Player.MediaTimeInfo mediaTimeInfo) {

                }
            });
            mediaPlayer.setVolume(BEEP_VOLUME);
            mediaPlayer.prepare();
            return mediaPlayer;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
